#!/bin/bash
#/*
# * Copyright 2017 ~ 2025 the original author or authors. <Wanglsir@gmail.com, 983708408@qq.com>
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# *      http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */

[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0

# Global definition.
. /etc/profile

# Check environment.
[ -z "$MYSQL_HOME" ] && echo "Please check set MYSQL_HOME environment." && exit -1
[ -z "$MYSQL_USER" ] && echo "Please check set MYSQL_USER environment." && exit -1
[ -z "$MYSQL_GROUP" ] && echo "Please check set MYSQL_USER environment." && exit -1
[ -z "$MYSQL_PLUGIN_DIR" ] && echo "Please check set MYSQL_PLUGIN_DIR environment." && exit -1
[ -z "$MYSQL_CONF_FILE" ] && echo "Please check set MYSQL_CONF_FILE environment." && exit -1
[ -z "$MYSQL_WORKSPACE_DIR" ] && echo "Please check set MYSQL_WORKSPACE_DIR environment." && exit -1
[ -z "$MYSQL_DATA_DIR" ] && echo "Please check set MYSQL_DATA_DIR environment." && exit -1
[ -z "$MYSQL_BINLOG" ] && echo "Please check set MYSQL_BINLOG environment." && exit -1
[ -z "$MYSQL_LOG_DIR" ] && echo "Please check set MYSQL_LOG_DIR environment." && exit -1
[ -z "$MYSQL_PID_FILE" ] && echo "Please check set MYSQL_PID_FILE environment." && exit -1
[ -z "$MYSQL_SOCKET" ] && echo "Please check set MYSQL_SOCKET environment." && exit -1

function start() {
  local isDebug=$1
  local pids=$(getPids)
  if [ -z "$pids" ]; then
    local START_CMD="${MYSQL_HOME}/bin/mysqld \
--defaults-file=${MYSQL_CONF_FILE} \
--basedir=${MYSQL_HOME} \
--datadir=${MYSQL_DATA_DIR} \
--plugin-dir=${MYSQL_PLUGIN_DIR} \
--user=${MYSQL_USER} \
--log-error=${MYSQL_LOG_DIR}/mysqld.err \
--log_bin=${MYSQL_BINLOG} \
--pid-file=${MYSQL_PID_FILE} \
--socket=${MYSQL_SOCKET}"
#--skip-grant-tables

    # Print debug.
    [ -n "$isDebug" ] && echo "Starting for - $START_CMD"

    if [ "$PPID" == "1" ]; then # Systemd call.
      $START_CMD
    else # Normal shell call.
      su - "$MYSQL_USER" -f -c "nohup $START_CMD 1> $MYSQL_LOG_DIR/mysqld.stdout 2> $MYSQL_LOG_DIR/mysqld.stderr &"
    fi

    echo -n "Starting mysqld ..."
    while true
    do
      pids=$(getPids)
      if [ "$pids" == "" ]; then
        echo -n ".";
        sleep 0.8;
      else
        break
      fi
    done
    echo -e "\nStarted mysqld on "$pids
  else
    echo "mysqld process is running "$pids
  fi
}

function stop() {
  local pids=$(getPids)
  if [ -z "$pids" ]; then
    echo "mysqld not running!"
  else
    echo -n "Stopping mysqld for $pids ..."
    kill -s TERM $pids
    while true
    do
      pids=$(getPids)
      if [ "$pids" == "" ]; then
        break
      else
        echo -n ".";
        sleep 0.8;
      fi
    done
    echo -e "\nStopped mysqld !"
  fi
}

function status() {
  ps -ef | grep -v grep | grep -i "$MYSQL_HOME" | grep $MYSQL_DATA_DIR
}

function getPids() {
  local pids=$(ps ax | grep -i "$MYSQL_HOME" | grep -i "$MYSQL_DATA_DIR" | grep -v grep | awk '{print $1}')
  echo $pids # Output execution result value.
  return 0 # Return the execution result code.
}

# --- Main call. ---
case $1 in
  status)
    status
    ;;
  start)
    start $2
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start $2
    ;;
    *)
  echo $"Usage: {start [--debug]|stop|restart|status}"
  exit 2
esac